package v1

import (
	router2 "apibe/grpc/rome/v1/router"
	"apibe/grpc/rome/v1/share/config"
	middler2 "apibe/grpc/rome/v1/share/middler"
	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
	_ "net/http/pprof"
)

type Option struct {
}

func Init(option ...Option) {
	e := gin.Default()
	pprof.Register(e)
	gin.SetMode(config.Config.Mode)
	all(e) // 端口注册
	err := e.Run(config.Config.Net.Port)
	if err != nil {
		panic(err.Error())
	}
}

const v1 = "v1"

func all(e *gin.Engine) {
	e.Use(middler2.CrossMiddleware)
	group := e.Group(v1)
	// design 接口
	{
		design := group.Group("/design")
		design.POST("/process/:api_name", middler2.JWTMiddleware, router2.Design) // 转换接口
		design.POST("/upsert", middler2.ApiMiddleware, middler2.CryptMiddleware, middler2.JWTMiddleware, router2.DesignUpsert)
		design.POST("/findMany", middler2.ApiMiddleware, middler2.CryptMiddleware, middler2.JWTMiddleware, router2.DesignFindMany)
	}
	// task 接口
	{
		task := group.Group("/task")
		task.POST("/upsert", middler2.ApiMiddleware, middler2.CryptMiddleware, middler2.JWTMiddleware, router2.TaskUpsert)
		task.POST("/findMany", middler2.ApiMiddleware, middler2.CryptMiddleware, middler2.JWTMiddleware, router2.TaskFindMany)
	}
	// user 接口
	{
		user := group.Group("/user")
		user.POST("/register", middler2.ApiMiddleware, middler2.CryptMiddleware, router2.UserRegister)
		user.POST("/login", middler2.ApiMiddleware, middler2.CryptMiddleware, router2.UserLogin)
		user.POST("/update", middler2.ApiMiddleware, middler2.CryptMiddleware, middler2.JWTMiddleware, router2.UserUpdate)
		user.POST("/findMany", middler2.ApiMiddleware, middler2.CryptMiddleware, middler2.JWTMiddleware, router2.UserFindMany)
	}
	// api接口
	{
		api := group.Group("/api")
		api.GET("/getWatch/:api_name", middler2.ApiMiddleware, router2.ApiGetWatch)
		api.POST("/selector/:action/:selector", middler2.ApiMiddleware, router2.ApiSelector)
		api.POST("/:work/:action/:id", middler2.ApiMiddleware, middler2.JWTMiddleware, middler2.CryptMiddleware, router2.ApiChangeStatus)
		api.POST("/cache_response/:action/:id", middler2.ApiMiddleware, middler2.CryptMiddleware, router2.ApiCacheResponse)
		api.POST("/postman/export", middler2.JWTMiddleware, middler2.CryptMiddleware, router2.ApiExportPostman)
	}
	// tourist 数据库直接操作接口
	{
		tourist := group.Group("/tourist", middler2.WhiteMiddleware)
		// to mongo
		mongo := tourist.Group("/mongo", middler2.ApiMiddleware, middler2.CryptMiddleware)
		mongo.PUT("/create_one", router2.MongoCreateOne)
		mongo.PUT("/create_many", router2.MongoCreateMany)
		mongo.GET("/query_one", router2.MongoQueryOne)
		mongo.GET("/query_many", router2.MongoQueryMany)
		mongo.POST("/update_one", router2.MongoUpdateOne)
		mongo.POST("/update_many", router2.MongoUpdateMany)
		mongo.DELETE("/remove_one", router2.MongoRemoveOne)
		mongo.DELETE("/remove_many", router2.MongoRemoveMany)
		// to redis
		redis := tourist.Group("/redis", middler2.ApiMiddleware, middler2.CryptMiddleware)
		redis.GET("/get_one", router2.RedisGetOne)
		redis.GET("/get_many", router2.RedisGetMany)
		redis.PUT("/set_one", router2.RedisSetOne)
		redis.PUT("/set_many", router2.RedisSetMany)
		redis.DELETE("/del_one", router2.RedisRemoveOne)
		redis.DELETE("/del_many", router2.RedisRemoveMany)
		// to mysql
		mysql := tourist.Group("/mysql", middler2.ApiMiddleware, middler2.CryptMiddleware)
		mysql.GET("/select_one", router2.MysqlSelectOne)
		mysql.GET("/select_many", router2.MysqlSelectMany)
		mysql.POST("/exec_one", router2.MysqlExecOne)
		mysql.POST("/exec_many", router2.MysqlExecMany)
	}
}
